package JDBC;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

public class DBHelper {
    public static String DRIVER ="com.mysql.jdbc.Driver";//数据库
    public static final String URL ="jdbc:mysql://localhost:3306/shopping?useSSL=false";
//    public final static String DRIVER = "com.mysql.jdbc.Driver";
//    public final static String URL = "jdbc:mysql://localhost:3306/shopping?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone = GMT";
    public static final String DBUser = "root";
    public static final String DBPassword = "123456";
    public static Connection conn = null;

    public static Connection getConn() {
        try {
            Class.forName(DRIVER);
            conn = DriverManager.getConnection(URL, DBUser, DBPassword);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return conn;
    }

    public static void closeAll(Connection conn, PreparedStatement pstmt, ResultSet rs) {
        if (rs != null) {
            try {
                rs.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        if (pstmt != null) {
            try {
                pstmt.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        if (conn != null) {
            try {
                conn.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}

